﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Windows.Forms;

namespace SCMS
{
    public class DAOManager
    {
        public enum DAOProviderType
        {
            SQL, MySql
        }

        public enum DAOApplicationType
        {
            Normal,
            Other,
            NormalOnline,
            OtherOnline
        }

        private Database mDataBase = null;
        private DbConnection mDbConnection = null;
        private DbTransaction mDbTransaction = null;

        public static string ConnectionName = "";
        public static string ConnectionString = "";
        public static DAOProviderType ConnectionProvider;

        public DAOManager()
        {
            CreateDatabase();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aProvider"></param>
        /// <param name="aConnectionString"></param>
        public DAOManager(DAOProviderType aProvider, string aConnectionString)
        {
            switch (aProvider)
            {
                case DAOProviderType.SQL:
                    mDataBase = new GenericDatabase(aConnectionString, System.Data.SqlClient.SqlClientFactory.Instance);
                    break;
                case DAOProviderType.MySql:
                    mDataBase = new GenericDatabase(aConnectionString, MySql.Data.MySqlClient.MySqlClientFactory.Instance);
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aConnectionName"></param>
        /// <returns>Database Object</returns>
        public Database CreateDataBase(String aConnectionName)
        {
            try
            {
                mDataBase = DatabaseFactory.CreateDatabase(aConnectionName);
                return mDataBase;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// Create The DateBaseObject with the default configuration name.
        /// </summary>
        /// <returns>DataBase Object</returns>
        public Database CreateDatabase()
        {
            try
            {
                if (ConnectionString.Equals(string.Empty))
                {
                    mDataBase = DatabaseFactory.CreateDatabase(ConnectionName);
                }
                else
                {
                    switch (ConnectionProvider)
                    {
                        case DAOProviderType.SQL:
                            mDataBase = new GenericDatabase(ConnectionString, System.Data.SqlClient.SqlClientFactory.Instance);
                            break;
                        case DAOProviderType.MySql:
                            mDataBase = new GenericDatabase(ConnectionString, MySql.Data.MySqlClient.MySqlClientFactory.Instance);
                            break;
                        default:
                            break;
                    }
                }
                return mDataBase;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                throw e;
            }
        }

        public Database GetDataBase()
        {
            return mDataBase;
        }

        public void CreateConnection()
        {
            try
            {
                if (mDataBase == null)
                {
                    CreateDatabase();
                }
                mDbConnection = mDataBase.CreateConnection();

                mDbConnection.Open();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                throw;
            }
        }

        public DbConnection GetDbConnection()
        {
            if (mDbConnection == null)
            {
                CreateConnection();
            }
            return mDbConnection;
        }

        public DbDataAdapter GetDbAdapter()
        {
            try
            {
                return mDataBase.GetDataAdapter();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                throw e;
            }
        }

        public DbCommand GetDbCommand()
        {
            try
            {
                return mDataBase.DbProviderFactory.CreateCommand();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public DbCommandBuilder GetDbCommandBuilder()
        {
            try
            {
                return mDataBase.DbProviderFactory.CreateCommandBuilder();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                throw e;
            }
        }

        public DbTransaction GetDbTransaction()
        {
            return mDbTransaction;
        }

        public void BeginTransaction()
        {
            mDbTransaction = mDbConnection.BeginTransaction();
        }

        public void CommitTransaction()
        {
            mDbTransaction.Commit();
        }

        public void RollBackTransaction()
        {
            mDbTransaction.Rollback();
        }

        public void CloseConnection()
        {
            mDbConnection.Close();
        }

        public DataTable GetSchemaFields(string aTableName)
        {
            return mDbConnection.GetSchema("Columns");
        }

        public DataTable GetSchemaTable()
        {
            return mDbConnection.GetSchema("Tables");
        }
    }
}
